<template>
  <div class="container">
    <el-card v-loading="loading">
      <!-- 头部搜索区域 -->
      <el-row type="flex">
        <el-col>
          <el-form :inline="true">
            <el-form-item label="学科名称">
              <el-input
                v-model="requestParams.subjectName"
                style="width:190px"
                label="请输入搜索名称"
              />
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="clean">清除</el-button>
              <el-button size="small" type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col>
          <el-row type="flex" justify="end">
            <el-button
              icon="el-icon-edit"
              size="small"
              type="success"
              @click="add"
            >新建学科</el-button>
          </el-row>
        </el-col>
      </el-row>
      <!-- 警告消息提示区域 -->
      <el-alert
        :title="`数据一共${pages}条`"
        type="info"
        show-icon
        :closable="false"
      />
      <!-- 主体表格区域 -->
      <el-table
        :data="form"
        style="width: 100%;margin-top:20px"
        border
        center
      >
        <el-table-column
          align="center"
          label="序号"
          width="80"
          type="index"
          :index="indexMethod"
        />
        <el-table-column
          align="center"
          prop="subjectName"
          label="学科名称"
          width="120"
        />
        <el-table-column
          align="center"
          prop="creatorID"
          label="创建者"
          width="120"
        >
          <template v-slot="{row}">
            {{ role.find(item=>+item.id ===row.creatorID).value }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="addDate"
          label="创建日期"
          width="200"
        >
          <template v-slot="{row}">
            {{ row.addDate|time }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="isFrontDisplay"
          label="前台是否显示"
          width="120"
        >
          <template v-slot="{row}">
            {{ row.isFrontDisplay === 1?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="twoLevelDirectory"
          label="二级目录"
          width="120"
        />
        <el-table-column
          align="center"
          prop="tags"
          label="标签"
          width="120"
        />
        <el-table-column
          align="center"
          prop="totals"
          label="题目数量"
          width="120"
        />
        <el-table-column
          align="center"
          prop="address"
          label="操作"
        >
          <template v-slot="{row}">
            <el-button type="text" @click="$router.push(`directorys?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科分类</el-button>
            <el-button type="text" @click="$router.push(`tags?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科标签</el-button>
            <el-button type="text" @click="edit(row)">修改</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end" style="margin-top:15px">
        <el-pagination
          background
          :page-sizes="[5,10, 20, 30, 40, 50]"
          :page-size="requestParams.pagesize"
          layout="total,prev, pager, next, sizes,jumper"
          :total="pages"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-row>
    </el-card>
    <AddSubject
      ref="AddSubject"
      :edit-list="editList"
      @getList="getSubjectList"
    />
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects'
// 使用枚举，修改启用状态
import creator from '@/api/base/baseApi'
import AddSubject from '@/module-hmmm/components/subjects-add.vue'
export default {
  components: {
    AddSubject
  },
  data() {
    return {
      form: [],
      requestParams: {
        page: 1, // 当前页码
        pagesize: 10, // 每页条目数
        subjectName: ''
      },
      editList: {}, // 修改的内容
      pages: 0, // 总条目数
      loading: false, //
      role: creator.role, // 创建人的枚举数据
      showDialog: false//
    }
  },
  // 一进入页面调用方法发送请求，获取数据
  created() {
    this.getSubjectList()
  },
  methods: {
    // 分页换页时的执行请求
    handleCurrentChange(val) {
      // BUG修复，输入框输入内容不搜索，换页及切条数时，没有数据
      this.requestParams.subjectName = ''
      this.requestParams.page = val
      this.getSubjectList()
    },
    // 切换每页条数时的执行请求
    handleSizeChange(val) {
      this.requestParams.subjectName = ''
      this.requestParams.pagesize = val
      this.getSubjectList()
    },
    // 自定义序号
    indexMethod(index) {
      return (this.requestParams.page - 1) * this.requestParams.pagesize + index + 1
    },
    // 获取学科列表的请求
    async getSubjectList() {
      this.loading = true
      const { data } = await list(this.requestParams)
      this.form = data.items
      this.pages = data.counts
      this.loading = false
    },
    // 清除输入框按钮
    clean() {
      this.requestParams = {
        page: 1,
        pagesize: 10,
        subjectName: ''
      }
    },
    // 搜索按钮
    search() {
      this.getSubjectList()
    },
    // 添加功能，弹出添加对话框
    add() {
      this.$refs.AddSubject.showDialog = true
    },
    // 删除功能
    async del(id) {
      try {
        await this.$confirm('你确认要删除此条学科信息吗？', '温馨提示', {
          type: 'warning'
        })
      } catch {
        this.loading = false
        return
      }
      await remove({
        id
      })
      this.loading = true
      this.$message.success('删除成功')
      this.loading = false
      this.getSubjectList()
    },
    // 修改功能
    edit(row) {
      console.log(row)
      this.$refs.AddSubject.showDialog = true
      this.$refs.AddSubject.addInfo = { ...row }
      this.editList = { ...row }
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 20px;

}
</style>
